package ru.bragnikita.utils.logexplorer.web;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.primefaces.model.SelectableDataModel;
import ru.bragnikita.utils.logexplorer.model.FilesSet;
import ru.bragnikita.utils.logexplorer.model.FsItem;

import javax.annotation.Nullable;
import java.util.List;

/**
 * @author Nikita.Brazhnikov
 */
public class DirectoryViewModel implements SelectableDataModel<FsItem> {

	private List<FsItem> items;

	public DirectoryViewModel(FilesSet filesSet) {
		items = Lists.newArrayListWithCapacity(filesSet.getItems().size() + 1);
		for (FsItem fsItem : filesSet.getItems()) {
			items.add(fsItem);
		}
	}

	public List<FsItem> getItems() {
		return items;
	}

	@Override
	public Object getRowKey(FsItem object) {
		return object.getAbsolutePath();
	}

	@Override
	public FsItem getRowData(final String rowKey) {
		return Iterables.find(items, new Predicate<FsItem>() {
			@Override
			public boolean apply(@Nullable FsItem input) {
				return input.getAbsolutePath().equals(rowKey);
			}
		});
	}
}
